<?php
namespace Home\Controller;
use Think\Controller;

class IndexController extends Controller {
    public function __construct(){
        parent::__construct();
        $this->token = isset($_GET['token']) ? $_GET['token'] : 'dvaglg1469847559';
    }



    //注册
    public function register(){
        if(IS_POST){
//            $code = M('AssetsCode')->where(array('tel' => $_POST['tel']))->getField('code');
            if($_POST['code'] == session('code')){
                $user = M('AssetsUser');
                $_POST['createtime'] = time();
                $_POST['token'] = $this->token;
                $_POST['password'] = md5($_POST['password']);
                $id = $user->add($_POST);
                if($id){
                    session('uid',$id);
                    $re_data['status'] =1;
                    $re_data['msg'] = '注册成功';
                }else{
                    $re_data['status'] =0;
                    $re_data['msg'] = '注册失败';
                }
            }else{
                $re_data['status'] =0;
                $re_data['msg'] = '验证码错误';
            }
            $this->ajaxReturn($re_data);
        }
        $this->display();
    }

    //获取验证码
    public function getsms(){
        if(IS_POST){
            $tel = $_POST['tel'];
            $userinfo = M('AssetsUser')->where(array('tel' => $tel,'token' => $this->token))->find();
            if(!$userinfo){
//                import('Org.SMS1');
                $sms_helper = new \Org\SMS1\SMS1();
                $param = array(
                    'code'      => (string)rand(10000,99999),
                    'product'   => '九铭资管',
                );
                $sms_helper->sendSMS($tel, "SMS_2695150", json_encode($param));
                session('code',$param['code']);
                $data['status'] = 1;
                $data['msg'] = '验证码发送成功';
            }else{
                $data['status'] = 0;
                $data['msg'] = '对不起该手机号已注册！';
            }
            $this->ajaxReturn($data);
        }

    }

    //登陆
    public function login(){
        if(IS_POST){
            $userinfo = M('AssetsUser')->where(array('tel' => $_POST['tel']))->find();
            if($userinfo){
                if($userinfo['password'] == md5($_POST['password'])){
                    session('uid',$userinfo['id'],3600*24);
                    if($userinfo['isinvestigation'] == 0){
                        $re_data['isinvestigation'] = 0;
                    }
                    $re_data['status'] = 1;
                    $re_data['msg'] = '登陆成功！';
                }else{
                    $re_data['status'] = 0;
                    $re_data['msg'] = '密码错误！';
                }
            }else{
                $re_data['status'] = 0;
                $re_data['msg'] = '用户不存在！';
            }
            $this->ajaxReturn($re_data);
        }
        $this->display();
    }

    //忘记密码
    public function password_back($step = null){
        if(IS_POST){
            $tel = $_POST['tel'];
            $userinfo = M('AssetsUser')->where(array('tel' => $tel,'token' => $this->token))->find();
            if($userinfo){
                if($step == 1){
                    $sms_helper = new \Org\SMS1\SMS1();
                    $param = array(
                        'code'      => (string)rand(10000,99999),
                        'product'   => '九铭资管',
                    );
                    $sms_helper->sendSMS($tel, "SMS_2695150", json_encode($param));
                    session('code_password',$param['code']);
                    $data['status'] = 1;
                    $data['msg'] = '验证码发送成功';
                }elseif($step == 2){
                    if(session('code_password') == $_POST['code']){
                        $data['status'] = 1;
                        $data['msg'] = '验证码正确！';
                    }else{
                        $data['status'] = 0;
                        $data['msg'] = '验证码错误！';
                    }
                }elseif($step == 3){
                    $password = md5($_POST['password']);
                    $result = M('AssetsUser')->where(array('tel' => $tel,'token' => $this->token))->setField('password',$password);
                    if($result){
                        $data['status'] = 1;
                        $data['msg'] = '修改密码成功！';
                    }else{
                        $data['status'] = 0;
                        $data['msg'] = '修改密码失败！';
                    }
                }
            }else{
                $data['status'] = 0;
                $data['msg'] = '对不起您还未注册！';
            }
            $this->ajaxReturn($data);
        }
        $this->display();
    }

    //退出登陆
    public function logout($isWeb = 1){
        session(null);
        $re_data['status'] = 1;
        $re_data['msg'] = '退出登陆成功！';
        $this->ajaxReturn($re_data);
    }


    //空操作
    public function _empty(){
        header('Location: /404.html');
    }
}